
var ImgUploadFile = Class.create();
ImgUploadFile.prototype = {
	initialize: function() {
		this.init();
	},	
	 init: function(){
	 	// 当前页面的domain
		this.uploadDomain = 'sohu.com';
        // 是否有附件正在上传
        this.uploading = false;
		// 记录当前正在上传的附件ID，Name
		this.currentFile = {filename: ""};
	 	this.uploadImageIframe = '<iframe name="file_upload_image" id="file_upload_image" src="javascript:document.open(\'text\/html\',\'replace\');document.domain=\''+ this.uploadDomain +'\';document.write(\'<head><meta http-equiv=Content-Type content=text/html;charset=utf-8 /></head><body><div><span> &nbsp; </span></div></body>\');document.close();" style="border:0px;width:0px;height:0px;position:absolute;left:-6000px;top:-6000px;" frameborder=0></iframe>';
	    // 初始化上传图片的iframe
		this.createImageIframe();
		this.signObj=null;
	 },
	 imageUpload: function(fobj) {
		var fname = fobj.upload_image.value;
		//console.log(fname);
		
		var uploadInput = '<input type="file" name="upload_image" id="upload_image" />&nbsp;&nbsp;<input name="button" type="button"  class="yes" value="上 传" onclick="mailUploadFile.imageUpload(this.parentNode);"/>';
		var filterName = ["jpg", "gif"];
		if (!fname.blank()) {
			fname = fname.substring(fname.lastIndexOf('\\') + 1, fname.length);
		}
		try {
			var e = fname.substring(fname.lastIndexOf('.') + 1, fname.length);
			if (!filterName.include(e.toLowerCase())) {
				setTimeout(function() {
					fobj.innerHTML = uploadInput;
				}, 10);
				alert('上传的图片格式只能是 "jpg", "gif" 这些格式的');
				$('upload_image').disabled=false;
				$('uploadImage').disabled=false;
				$('upload_image').value="";
				return;
			}		
		}catch(e) {}
		if (this.uploading == false) {
			this.uploading = true;
			var that = this;
			//$('upload_image').disabled=true;
			setTimeout(function() {
				fobj.submit();
				that.setCurrentFile(fname);
				$('upload_image').setAttribute('disabled', true);
				$('uploadImage').setAttribute('disabled', true);
			}, 400);
		}
	},
	setCurrentFile: function(filename) {
		this.currentFile.filename = filename;
	},
	// 图片上传成功，从上传附件的返回iframe页面中调用成功函数
	// res: Object, JSON数据，里面返回了数据上传的成功与否
	imageUploadTwo: function(res) {
		var ret = res.evalJSON();
		img_filename=ret.filename;
		this.uploading = false;
		if (ret.is_success) {
			this.createEndImg(ret.filename);
			try {
				// 上传成功之后重置Iframe
				this.createImageIframe();
				this.setCurrentFile("");
			} 
			catch (e) {}
		} else {
				alert(ret.message);
				$('upload_image').disabled=false;
				$('upload_image').value="";
				$('uploadImage').disabled=false;
		}
	},
	// 创建上传成功的图片信息
	createEndImg: function(filename) {
		if(this.signObj == null) {
			this.signObj = new Cropper.ImgWithPreview();
		}
		$('uploadImage').disabled=false;
		$('upload_image').disabled=false;
		$('upload_image').value="";
		$('bgDiv').innerHTML = '<img id="signImage" style="border: 1px none rgb(204, 204, 204); z-index: 1; display: block;"/>';
		Element.show($('imgDiv'));
		$('signImage').setAttribute('src', SIGN_ROOT+'/'+filename+'?'+Math.random());
		this.signObj.init(
			'signImage',
				{ 
				minWidth: 148, 
				minHeight: 106,
				ratioDim: { x: 148, y: 106 },
				displayOnInit: true,
				previewWrap: 'previewArea',
				onEndCrop:onEndCrop
				}
			);
	},
	// 创建接受返回值的iframe
	createImageIframe: function() {
        try {
        	if ($('file_upload_image') && typeof(frames['file_upload_image']) == "object") {
        		try {
        			// for IE
        			window.frames['file_upload_image'].document.execCommand('Stop');
        		} catch(e) {}
        		Element.remove($('file_upload_image'));
        	}
            Element.insert(document.body, {bottom: this.uploadImageIframe});
        } catch (e) {}
        
	}	 	
};

var imgUploadFile = new ImgUploadFile();

function imageUploadSuccess(info){
	imgUploadFile.imageUploadTwo(info);
};
